package com.cn.wanxi.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.cn.wanxi.dao.UserDao;
import com.cn.wanxi.dao.impl.UserImpl;
import com.cn.wanxi.model.ResultModel;
import com.cn.wanxi.model.UserModel;
import com.cn.wanxi.service.UserService;

/**
 * 服务逻辑层调用dao层
 */
public class UserServiceImpl implements UserService {
    private UserDao userDao = new UserImpl();
    private ResultModel resultModel = new ResultModel();

    @Override
    public ResultModel login(UserModel userModel) {
        String result = "";
//        if (!userModel.getSessionCode().equals(userModel.getCode())) {
        if (false) {
            result="error-code";
        } else {
            UserDao userDao = new UserImpl();
// 通过用户名和密码查询 ,要么可以查询出来，要么没有数据
//        有数据：要么位一条 要么为多条  list  model
//        没有数据：null,""
//        findByUsernameAndPassword方法名  小驼峰
            UserModel model = userDao.findByUsernameAndPassword(userModel);
            if (model == null) {
                result="error";
            } else {
                result="success";
            }
        }
        return new ResultModel(result);

    }

    @Override
    public ResultModel add(UserModel userModel) {
//        int add = userDao.add(userModel);
//        resultModel.setData(add);
        return new ResultModel(0, userDao.add(userModel));
    }

    @Override
    public ResultModel update(UserModel userModel) {
//        resultModel.setData(userDao.update(userModel));
        return new ResultModel(userDao.update(userModel));
    }

    @Override
    public ResultModel delete(UserModel userModel) {
//        resultModel.setData(userDao.delete(userModel));
        return new ResultModel(userDao.delete(userModel));
    }

    @Override
    public ResultModel findAll(UserModel userModel) {
        resultModel.setData(userDao.findAll(userModel));
        return resultModel;
    }

    @Override
    public ResultModel findById(UserModel userModel) {
        resultModel.setData(userDao.findById(userModel));
        return resultModel;
    }

    @Override
    public ResultModel getCount(UserModel userModel) {
        resultModel.setData(userDao.getCount(userModel));
        return resultModel;
    }
}
